package com.rr.openApi.article.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.rr.frame.common.FinalDes;
import com.rr.frame.exception.ServiceException;
import com.rr.frame.utils.InitRequestParameter;
import com.rr.openApi.article.service.ArticleCommentService;

@RestController
@RequestMapping("/openApi/articleComment")
public class ArticleCommentController {
	
	private Logger logger = Logger.getLogger(ArticleCommentController.class);
	
	@Autowired
	private ArticleCommentService articleCommentService;
	
	@RequestMapping(value="/getCommentList",method=RequestMethod.POST)
	 public Map<String,Object> getCommentList(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("获取文章评论参数输入=====>" + paraMap);
	            if(null==paraMap.get("aid")||"".equals(paraMap.get("aid"))){
		 			throw new ServiceException("FA_FAILED_AID_NOT_NULL", "文章id不能为空");
		 		}
	            if(null==paraMap.get("start")||"".equals(paraMap.get("start"))){
		 			paraMap.put("start", 0);
		 		}
	            if(null==paraMap.get("num")||"".equals(paraMap.get("num"))){
		 			paraMap.put("num", 20);
		 		}
	           List<Map<String, Object>> data = articleCommentService.getArticleCommentList(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("获取文章评论参数输出=====>" + resultMap);
	     return resultMap;
	}
	
	@RequestMapping(value="/addArticleComment",method=RequestMethod.POST)
	 public Map<String,Object> addComment(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("添加文章评论参数输入=====>" + paraMap);
	            if(null==paraMap.get("aid")||"".equals(paraMap.get("aid"))){
	 				throw new ServiceException("FA_FAILED_AID_NOT_NULL", "文章aid不能为空");
	 			}
	            if(null==paraMap.get("comment")||"".equals(paraMap.get("comment"))){
		 			throw new ServiceException("FA_FAILED_COMMENT_NOT_NULL", "评论内容不能为空");
		 		}
	            if(null==paraMap.get("uid")||"".equals(paraMap.get("uid"))){
		 			throw new ServiceException("FA_FAILED_UID_NOT_NULL", "评论者的uid不能为空");
		 		}
	            if(null==paraMap.get("belong_uid")||"".equals(paraMap.get("belong_uid"))){
		 			throw new ServiceException("FA_FAILED_BELONG_UID_NOT_NULL", "被评论者的uid不能为空");
		 		}
	           String data = articleCommentService.addArticleComment(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("添加文章评论参数输出=====>" + resultMap);
	     return resultMap;
	}
	
	@RequestMapping(value="/addArticleReply",method=RequestMethod.POST)
	 public Map<String,Object> addReply(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("添加文章评论参数输入=====>" + paraMap);
	            if(null==paraMap.get("aid")||"".equals(paraMap.get("aid"))){
	 				throw new ServiceException("FA_FAILED_AID_NOT_NULL", "文章aid不能为空");
	 			}
	            if(null==paraMap.get("comment")||"".equals(paraMap.get("comment"))){
		 			throw new ServiceException("FA_FAILED_COMMENT_NOT_NULL", "回复内容不能为空");
		 		}
	            if(null==paraMap.get("uid")||"".equals(paraMap.get("uid"))){
		 			throw new ServiceException("FA_FAILED_UID_NOT_NULL", "回复者的uid不能为空");
		 		}
	            if(null==paraMap.get("belong_uid")||"".equals(paraMap.get("belong_uid"))){
		 			throw new ServiceException("FA_FAILED_BELONG_UID_NOT_NULL", "被回复者的uid不能为空");
		 		}
	            if(null==paraMap.get("belong")||"".equals(paraMap.get("belong"))){
		 			throw new ServiceException("FA_FAILED_BELONG_NOT_NULL", "被回复的评论的cid不能为空");
		 		}
	           String data = articleCommentService.addArticleReply(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("添加文章评论参数输出=====>" + resultMap);
	     return resultMap;
	}
	
	@RequestMapping(value="/addLike",method=RequestMethod.POST)
	 public Map<String,Object> addLike(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("评论addLike参数输入=====>" + paraMap);
	            if(null==paraMap.get("cid")||"".equals(paraMap.get("cid"))){
		 			throw new ServiceException("FA_FAILED_AID_NOT_NULL", "文章id不能为空");
		 		}
	           String data = articleCommentService.addLike(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("评论addLike参数输出=====>" + resultMap);
	     return resultMap;
	}
	
}	


